/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.sys.service.user;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.hk.sys.entity.user.HkUserProductFreight;
import com.thinkgem.jeesite.modules.hk.sys.dao.user.HkUserProductFreightDao;
import com.thinkgem.jeesite.modules.hk.sys.entity.user.HkUserProductAreaFreight;
import com.thinkgem.jeesite.modules.hk.sys.dao.user.HkUserProductAreaFreightDao;

/**
 * 用户产品运费模板Service
 * @author 罗天文
 * @version 2018-06-20
 */
@Service
@Transactional(readOnly = true)
public class HkUserProductFreightService extends CrudService<HkUserProductFreightDao, HkUserProductFreight> {

	@Autowired
	private HkUserProductAreaFreightDao hkUserProductAreaFreightDao;
	
	public HkUserProductFreight get(String id) {
		HkUserProductFreight hkUserProductFreight = super.get(id);
		hkUserProductFreight.setHkUserProductAreaFreightList(hkUserProductAreaFreightDao.findList(new HkUserProductAreaFreight(hkUserProductFreight)));
		return hkUserProductFreight;
	}
	
	public List<HkUserProductFreight> findList(HkUserProductFreight hkUserProductFreight) {
		return super.findList(hkUserProductFreight);
	}
	
	public Page<HkUserProductFreight> findPage(Page<HkUserProductFreight> page, HkUserProductFreight hkUserProductFreight) {
		return super.findPage(page, hkUserProductFreight);
	}
	
	@Transactional(readOnly = false)
	public void save(HkUserProductFreight hkUserProductFreight) {
		super.save(hkUserProductFreight);
		for (HkUserProductAreaFreight hkUserProductAreaFreight : hkUserProductFreight.getHkUserProductAreaFreightList()){
			if (hkUserProductAreaFreight.getId() == null){
				continue;
			}
			if (HkUserProductAreaFreight.DEL_FLAG_NORMAL.equals(hkUserProductAreaFreight.getDelFlag())){
				if (StringUtils.isBlank(hkUserProductAreaFreight.getId())){
					hkUserProductAreaFreight.setFreight(hkUserProductFreight);
					hkUserProductAreaFreight.preInsert();
					hkUserProductAreaFreightDao.insert(hkUserProductAreaFreight);
				}else{
					hkUserProductAreaFreight.preUpdate();
					hkUserProductAreaFreightDao.update(hkUserProductAreaFreight);
				}
			}else{
				hkUserProductAreaFreightDao.delete(hkUserProductAreaFreight);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(HkUserProductFreight hkUserProductFreight) {
		super.delete(hkUserProductFreight);
		hkUserProductAreaFreightDao.delete(new HkUserProductAreaFreight(hkUserProductFreight));
	}
	
}